﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using CLEXA.DataContracts;

namespace CLEXA.AdminClient.ViewModel
{
    internal sealed class UserCreateVM : VMBase
    {
        private readonly IFacade facade;
        private RelayCommand create;
        private RelayCommand cancel;
        private string username;
        private string password;
        private bool isActive;

        public ICommand Create
        {
            get
            {
                if (create == null)
                {
                    create = new RelayCommand(DoCreate);
                }
                return create;
            }
        }

        public ICommand Cancel
        {
            get
            {
                if (cancel == null)
                {
                    cancel = new RelayCommand(DoCancel);
                }
                return cancel;
            }
        }

        public string Username
        {
            get { return username; }
            set
            {
                if (username != value)
                {
                    username = value;
                    RaisePropertyChanged("Username");
                }
            }
        }

        public string Password
        {
            get { return password; }
            set
            {
                if (password != value)
                {
                    password = value;
                    RaisePropertyChanged("Password");
                }
            }
        }

        public bool IsActive
        {
            get { return isActive; }
            set
            {
                if (isActive != value)
                {
                    isActive = value;
                    RaisePropertyChanged("IsActive");
                }
            }
        }

        public UserCreateVM(IFacade facade)
        {
            this.facade = facade;
        }

        private void DoCreate()
        {
            try
            {
                facade.UserCreate(username, password, isActive);
            }
            catch (Exception ex)
            {
                facade.ShowErrorDialog("An unexpected error occured while creating the user.", ex);
                return;
            }
            facade.CloseWindow(this);
        }

        private void DoCancel()
        {
            facade.CloseWindow(this);
        }
    }
}
